package com.xf.sys.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Service;

import com.xf.pm.common.PageData;
import com.xf.pm.common.PageForm;
import com.xf.pm.util.PageUtils;
import com.xf.sys.entity.SysResource;
import com.xf.sys.entity.SysResourcesRole;
import com.xf.sys.entity.TreeNode;
import com.xf.sys.mapper.SysResourceMapper;
import com.xf.sys.mapper.SysResourcesRoleMapper;
import com.xf.sys.service.ISysResourceService;

/**
 * xf-sys-service
 * SysResourceService.java
 * author:lb
 * 2017年7月4日
 */
@Service
public class SysResourceService implements ISysResourceService {
	
	private final Logger logger =  LoggerFactory.getLogger(this.getClass());

	@Autowired
	private SysResourceMapper sysResourceMapper;
	
	@Autowired
	private SysResourcesRoleMapper sysResourcesRoleMapper;
	
	@Autowired
	private RedisTemplate<String, String> redisTemplate;
	
	@Override
	public int insertSysResource(SysResource sysResource) {
		logger.info("添加资源开始");
		RedisAtomicLong sysResourceId = new RedisAtomicLong("sysResourceId", redisTemplate.getConnectionFactory());
		if(null!=sysResource){
			sysResource.setResourceId((int)sysResourceId.incrementAndGet());
			sysResourceMapper.insertSysResource(sysResource);
		}
		return 0;
	}

	@Override
	public int deleteSysResource(Integer[] resourceId) {
		logger.info("删除资源开始");
		return sysResourceMapper.deleteSysResource(resourceId);
	}

	@Override
	public int updateSysResource(SysResource sysResource) {
		logger.info("修改资源开始");
		return sysResourceMapper.updateSysResource(sysResource);
	}

	@Override
	public PageData selectSysResourceList(PageForm form, SysResource sysResource) {
		logger.info("查询资源列表开始");
		PageUtils.preparePaging(form);
		List<SysResource> list = null;
		PageData listData = null;
		try {
			list =sysResourceMapper.selectSysResourceList(sysResource);
			listData = PageUtils.wrapPageData(list);
		} catch (Exception e) {
			logger.info("查询系統用戶数据失败!");
			e.printStackTrace();
		}
		return listData;
	}

	@Override
	public SysResource selectSysResource(Integer resourceId) {
		logger.info("查询资源开始");
		return sysResourceMapper.selectSysResource(resourceId);
	}

	@Override
	public List<TreeNode> listTree(Integer menuId,Integer roleId) {
		List<TreeNode> trees = new ArrayList<TreeNode>();
		List<SysResource> list=sysResourceMapper.selectSysResourceMenuId(menuId);
		List<SysResourcesRole> srrList= sysResourcesRoleMapper.selectByRoleId(roleId);
		
		for(SysResource sysResource : list){
			TreeNode tree = new TreeNode();
			tree.setId(String.valueOf(sysResource.getResourceId()));
			tree.setName(sysResource.getResourceName());
			for(SysResourcesRole sysResourcesRole : srrList){
				if(sysResource.getResourceId()==(sysResourcesRole.getResourceId())){
					tree.setChecked(true);
					break;
				}
			}
//			tree.setIcon(sysResource.getIcon());
			trees.add(tree);
		}
		return trees;
	}

}
